//初始化swiper
var mySwiper = new Swiper('.swiper-container', {
    autoplay: 2500,//可选选项，自动滑动
    pagination : '.swiper-pagination',
    paginationType : 'bullets',
    nextButton: '.swiper-button-next',
    prevButton: '.swiper-button-prev',
    loop:true,
    autoplayDisableOnInteraction : false,
    paginationClickable :true,
    noSwipingClass:'swiper-slide'
});
$(".swiper-slide").mouseenter(function () {
    mySwiper.stopAutoplay();
}).mouseleave(function() {
    mySwiper.startAutoplay();
})
//登录注册点击关闭跳到选择登录还是注册
$('.to-choose').click(function(){
    $('.login').css('display','none');
    $('.reg').css('display','none');
    $('.choose-method').css('display','block');
})
//登录还是注册页面点击关闭消失
$('.close').click(function(){
    $('.mask').css('display','none');
})
//登录功能
//跳转到登录
$('.tologin-btn').click(function(){
    $('.choose-method').css('display','none');
    $('.reg-btn').css('display','none');
    $('.login').css('display','block');
})
//点击进行登录请求
$('.login-btn').click(function(){
    var mobile = $(".login-reg").val();
    var password = $(".login-password").val();
    var reg = new RegExp(/^((\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)$/);
    if(!mobile.match(reg)){
        $('.err-info').html('请输入正确的手机号或用户名')
    }
    if(!password){
        $('.err-info').html('请输入密码')
    }
    var userdata = {
        mobile:mobile,
        password:md5(password)
    }
    if(mobile && password){
        $.ajax({
            url:'/login.json',
            type: 'post',
            data:JSON.stringify(userdata),
            contentType:'application/json;charset=UTF-8',
            success: function (data) {
                if (data.stat == 'OK') {
                    location.href= '/index?viewType=page';
                }else{
                    $('.err-info').html("登录失败");
                }
            },
            error:function (err) {
                $('.err-info').html("服务器连接失败");
            }
        })
    }
})
//注册功能
//跳转到注册
$('.toreg-btn').click(function(){
    $('.choose-method').css('display','none');
    $('.reg').css('display','block');
    $('.login').css('display','none');
})
//验证码
//验证码倒计时
var countdown=120;
function settime(obj) {
if (countdown == 0) {
obj.removeAttribute("disabled");
obj.value="获取验证码";
countdown = 120;
return;
} else {
obj.setAttribute("disabled", true);
obj.value="重新发送(" + countdown + ")";
countdown--;
}
setTimeout(function() {
settime(obj) }
,1000)
}
var arr = [];
//获取验证码
$('.getCode').click(function(){
    var mobile = $('.reg-text').val();
    settime(this);
    $.ajax({
    url:"/code.json",
    type:"get",
     dataType:"json",
        data:{
             mobile:mobile
        },
    success:function(data){
         console.log(data)
         arr.verification = data;
         $('.err-info').html('验证码已发送');
       },
       error: function(msg) {
          $('.err-info').html('服务器连接失败，请稍后重试');
       }
     });
})
//点击进行注册
$('.reg-btn').click(function(){
    var regMobile =  new RegExp(/^((\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)$/);
    var regPassword = new RegExp(/^(?!(?:\d+|[a-zA-Z]+)$)[\da-zA-Z]{6,}$/);
    var mobile = $('.reg-text').val();
    var password = $('.reg-password').val();
    var confirm = $('.confirm').val();
    var verification = $('.code').val();
    if (!mobile.match(regMobile)) {
        $('.reg-err').html('请输入有效的手机号码');
        return false;
    }else if (!password.match(regPassword)) {
         $('.reg-err').html('密码应为6位以上非纯数字');
         return false;
     }else if(!(password==confirm)){
        $('.reg-err').html('与设置密码不一致，请确认您的密码');
        return false;
    }else if(!(arr.verification==verification)){
        $('.reg-err').html('验证码不正确');
        return false;
    }else{
        $('.reg-err').html('');
    }
    var data = {
            mobile : mobile,
            password : md5(password),
            repassword:md5(confirm),
            mobileCode:verification
        }
    //发送用户数据
    $.ajax({
        url: '/registration.json',
        type: 'post',
        data:JSON.stringify(data),
        contentType:'application/json;charset=UTF-8',
        crossDomain: true,
        success: function (data) {
            $('.reg-err').html('注册成功');
        },
        error:function (err) {
            $('.reg-err').html("服务器连接失败");
        }
    });
})


